package com.mastercard.ganadores.struts.Action;

import com.mastercard.ganadores.util.Dao.WinDAO;
import com.mastercard.ganadores.util.to.Util;
import com.mastercard.ganadores.util.to.boletoTo;
import com.mastercard.ganadores.util.to.userLogin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class loginAction extends Action {

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws
            IOException,
            ServletException {
        // en caos de null
        if (!Util.isLoggedIn(request)) {
            return mapping.findForward("noLogin");
        }

        // toma el userLogin
        userLogin user = Util.getUserLogin(request);

        // en caso de null
        if (user == null) {
            return mapping.findForward("noLogin");
        }
        
        System.out.println( "Entra al login java" ) ;
        boletoTo datos = null ;
        
        try {
            
            datos = WinDAO.datos( user.getUserName( ) , user.getPassword( ) ) ;
            
            request.setAttribute("datos", datos);
            
        } catch ( Exception e ) {
            e.printStackTrace( ) ;
        }
        
        return mapping.findForward( "success" ) ;
    }
    
}
